﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using System.Web.Routing;
using Ninject;
using Ninject.Modules;
using Ninject.Selection;

namespace SportStore.Core.IoC
{
    public class NinjectControllerFactory : DefaultControllerFactory
    {
        private readonly IKernel ninjectKernel;

        public NinjectControllerFactory()
        {
            ninjectKernel = new StandardKernel(new NinjectSettings {UseReflectionBasedInjection = true}, new InjectionModule());

            //Properties injection
            this.ninjectKernel.Components.Get<ISelector>().InjectionHeuristics.Add(new DefaultPropertyInjection(this.ninjectKernel));
            
            AddBindings();
        }

        protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
        {
            return controllerType == null
            ? null
            : (IController)ninjectKernel.Get(controllerType);
        }

        private static void AddBindings()
        {
            // put additional bindings here
        }
    }
}
